package vn.aptech.leaveManagementSystem.view.manager;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import vn.aptech.leaveManagementSystem.business.dto.UserLoginInfo;
import vn.aptech.leaveManagementSystem.business.model.MyModel;
import vn.aptech.leaveManagementSystem.view.util.ComboboxItem;

public class DemoApprovalApplicationFormForManager extends JFrame{
	private JTextField txtfApprovalIDApp;
	private JTextField txtfApprovalFirstName;
	private JTextField txtfApprovalLastName;
	private JTextField txtfApprovalDepartment;
	private JTextField txtfApprovalPosition;
	private JTextField txtfApprovalGender;
	private JTextField txtfApprovalBirthday;
	private JTextField txtfApprovalJoinDate;
	private JTextField txtfApprovalStartDate;
	private JTextField txtfApprovalEndDate;
	private MyModel mymodel;
	private JTextArea txtaApprovalReason;
	private JComboBox cbbApprovalStatus;
	private JButton btnApprovalOK;
	private JButton btnApprovalCancel;
	//
	private DemoManagerMainForm mainform;
	private int idAppParameter;
	//
	private Type type2;

	public DemoApprovalApplicationFormForManager(DemoManagerMainForm mainform,int idApp) {
		this.mainform=mainform;
		idAppParameter=idApp;
		//
		InitGUI();
		InitData();
		InitAction();
	}

	private void InitData() {
		mymodel=new MyModel();
		GetData(idAppParameter);
		
	}

	private void InitAction() {
		btnApprovalCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Close();
			}
		});	
		
		btnApprovalOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ComboboxItem item=(ComboboxItem)cbbApprovalStatus.getSelectedItem();
				try {
					mymodel.applications.updateApplicationStatus(idAppParameter, (Integer) item.getValue());
					Close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	public void Close()
	{	
		toBack();
		dispose();
		mainform.setEnabled(true);
		mainform.UpdateSource();
	}
	
	private void InitGUI() {
		// TODO Auto-generated method stub
		setBounds(100, 100, 450, 469);
		//------------------------------------------------------
		setCloseForm();
		//------------------------------------------------------
		setTitle("Approval Application Form");
		setLocationRelativeTo(null);
		getContentPane().setLayout(null);
		
		JLabel lblApprovalIDApp = new JLabel("ID Application:");
		lblApprovalIDApp.setBounds(10, 26, 97, 14);
		getContentPane().add(lblApprovalIDApp);
		
		JLabel lblApprovalFirstName = new JLabel("First Name:");
		lblApprovalFirstName.setBounds(10, 51, 97, 14);
		getContentPane().add(lblApprovalFirstName);
		
		JLabel lblApprovalLastName = new JLabel("Last Name:");
		lblApprovalLastName.setBounds(10, 78, 97, 14);
		getContentPane().add(lblApprovalLastName);
		
		JLabel lblApprovalDepartment = new JLabel("Department:");
		lblApprovalDepartment.setBounds(10, 177, 97, 14);
		getContentPane().add(lblApprovalDepartment);
		
		JLabel lblApprovalPosition = new JLabel("Position:");
		lblApprovalPosition.setBounds(10, 202, 97, 14);
		getContentPane().add(lblApprovalPosition);
		
		JLabel lblApprovalGender = new JLabel("Gender:");
		lblApprovalGender.setBounds(10, 103, 97, 14);
		getContentPane().add(lblApprovalGender);
		
		JLabel lblApprovalBirthday = new JLabel("Birthday:");
		lblApprovalBirthday.setBounds(10, 128, 97, 14);
		getContentPane().add(lblApprovalBirthday);
		
		JLabel lblApproval = new JLabel("Join date:");
		lblApproval.setBounds(10, 152, 97, 14);
		getContentPane().add(lblApproval);
		
		JLabel lblApprovalReason = new JLabel("Reason leaving:");
		lblApprovalReason.setBounds(10, 230, 97, 14);
		getContentPane().add(lblApprovalReason);
		
		JLabel lblStartDate = new JLabel("Start date:");
		lblStartDate.setBounds(10, 299, 97, 14);
		getContentPane().add(lblStartDate);
		
		JLabel lblApprovalEndDate = new JLabel("End date:");
		lblApprovalEndDate.setBounds(10, 324, 97, 14);
		getContentPane().add(lblApprovalEndDate);
		
		JLabel lblApprovalStatus = new JLabel("Status:");
		lblApprovalStatus.setBounds(10, 349, 97, 14);
		getContentPane().add(lblApprovalStatus);
		
		txtfApprovalIDApp = new JTextField();
		txtfApprovalIDApp.setEditable(false);
		txtfApprovalIDApp.setBounds(109, 23, 323, 20);
		getContentPane().add(txtfApprovalIDApp);
		txtfApprovalIDApp.setColumns(10);
		
		txtfApprovalFirstName = new JTextField();
		txtfApprovalFirstName.setEditable(false);
		txtfApprovalFirstName.setColumns(10);
		txtfApprovalFirstName.setBounds(109, 48, 323, 20);
		getContentPane().add(txtfApprovalFirstName);
		
		txtfApprovalLastName = new JTextField();
		txtfApprovalLastName.setEditable(false);
		txtfApprovalLastName.setColumns(10);
		txtfApprovalLastName.setBounds(109, 75, 323, 20);
		getContentPane().add(txtfApprovalLastName);
		
		txtfApprovalDepartment = new JTextField();
		txtfApprovalDepartment.setEditable(false);
		txtfApprovalDepartment.setColumns(10);
		txtfApprovalDepartment.setBounds(109, 174, 323, 20);
		getContentPane().add(txtfApprovalDepartment);
		
		txtfApprovalPosition = new JTextField();
		txtfApprovalPosition.setEditable(false);
		txtfApprovalPosition.setColumns(10);
		txtfApprovalPosition.setBounds(109, 199, 323, 20);
		getContentPane().add(txtfApprovalPosition);
		
		txtfApprovalGender = new JTextField();
		txtfApprovalGender.setEditable(false);
		txtfApprovalGender.setColumns(10);
		txtfApprovalGender.setBounds(109, 100, 323, 20);
		getContentPane().add(txtfApprovalGender);
		
		txtfApprovalBirthday = new JTextField();
		txtfApprovalBirthday.setEditable(false);
		txtfApprovalBirthday.setColumns(10);
		txtfApprovalBirthday.setBounds(109, 125, 323, 20);
		getContentPane().add(txtfApprovalBirthday);
		
		txtfApprovalJoinDate = new JTextField();
		txtfApprovalJoinDate.setEditable(false);
		txtfApprovalJoinDate.setColumns(10);
		txtfApprovalJoinDate.setBounds(109, 149, 323, 20);
		getContentPane().add(txtfApprovalJoinDate);
		
		txtfApprovalStartDate = new JTextField();
		txtfApprovalStartDate.setEditable(false);
		txtfApprovalStartDate.setColumns(10);
		txtfApprovalStartDate.setBounds(109, 296, 323, 20);
		getContentPane().add(txtfApprovalStartDate);
		
		txtfApprovalEndDate = new JTextField();
		txtfApprovalEndDate.setEditable(false);
		txtfApprovalEndDate.setColumns(10);
		txtfApprovalEndDate.setBounds(109, 321, 323, 20);
		getContentPane().add(txtfApprovalEndDate);
		
		txtaApprovalReason = new JTextArea();
		txtaApprovalReason.setEditable(false);
		txtaApprovalReason.setBounds(109, 228, 323, 61);
		getContentPane().add(txtaApprovalReason);
		
		cbbApprovalStatus = new JComboBox();
		cbbApprovalStatus.setBounds(109, 346, 323, 20);
		cbbApprovalStatus.requestFocusInWindow();
		getContentPane().add(cbbApprovalStatus);
		
		btnApprovalOK = new JButton("OK");
		btnApprovalOK.setIcon(new ImageIcon("icons/gif/16x16/OK.gif"));
		btnApprovalOK.setBounds(119, 387, 89, 23);
		getContentPane().add(btnApprovalOK);
		
		btnApprovalCancel = new JButton("Cancel");
		btnApprovalCancel.setIcon(new ImageIcon("icons/gif/16x16/Cancel.gif"));
		btnApprovalCancel.setBounds(218, 387, 108, 23);
		getContentPane().add(btnApprovalCancel);
		
		
		
	}
	
	public void GetData(int idApp)
	{
		try {
			ResultSet rs=mymodel.applications.getApplication(idApp);
			rs.next();
			txtfApprovalIDApp.setText(new Integer(rs.getInt("IDApplication")).toString());
			txtfApprovalFirstName.setText(rs.getString("FirstName"));
			txtfApprovalLastName.setText(rs.getString("LastName"));
			String strGender=null;
			if (rs.getBoolean("Gender")==true)
				strGender="Male";
			else
				strGender="Female";
			txtfApprovalGender.setText(strGender);
			txtfApprovalBirthday.setText(rs.getDate("Birthday").toString());
			txtfApprovalJoinDate.setText(rs.getDate("JoinDate").toString());
			txtfApprovalDepartment.setText(rs.getString("DepartmentName"));
			txtfApprovalPosition.setText(rs.getString("PositionName"));
			txtaApprovalReason.setText(rs.getString("Reason"));
			txtfApprovalStartDate.setText(rs.getDate("StartDate").toString());
			txtfApprovalEndDate.setText(rs.getDate("EndDate").toString());
			int status=rs.getInt("Status");
			if (status==0)
			{
				cbbApprovalStatus.addItem(new ComboboxItem(0, "Awaiting approval to Leave"));
				cbbApprovalStatus.addItem(new ComboboxItem(1, "Approve this leave application"));
				cbbApprovalStatus.addItem(new ComboboxItem(2, "Reject this leave application"));
			}
			if (status==4)
			{
				cbbApprovalStatus.addItem(new ComboboxItem(4, "Awaiting approval to Cancel Leaving"));
				cbbApprovalStatus.addItem(new ComboboxItem(5, "Approve to Cancel Leaving"));
				cbbApprovalStatus.addItem(new ComboboxItem(6, "Reject to Cancel Leaving"));
			}
			for (int i=0;i<cbbApprovalStatus.getItemCount();i++)
			{
				ComboboxItem item=(ComboboxItem) cbbApprovalStatus.getItemAt(i);
				if (status==(Integer)item.getValue())
				{
					cbbApprovalStatus.setSelectedItem(cbbApprovalStatus.getItemAt(i));
					break;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}	
	}
	
	public void setCloseForm()
	{
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				JFrame jframe=(JFrame) e.getSource();
				mainform.setEnabled(true);
				setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			}
		});
	}

}
